package com.fenghongzhang.sample.recycleview

import android.content.Context
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.fenghongzhang.sample.Food
import com.fenghongzhang.sample.R
import kotlinx.android.synthetic.main.activity_sample_recycle_view.*

/**
 * RecycleView 案例,部分代码备注看 listView中
 */
class SampleRecycleViewActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_sample_recycle_view)

        //数据源 mutableListOf是可以添加的list集合 listOf是固定长度的list结合
        val list = mutableListOf<Food>()
        repeat(20){
            list.add(Food(radmonTitle("苹果"),R.mipmap.ic_launcher_round))
        }

        val adapter =  FoodRecycleViewAdapter(list)
//        val manager = LinearLayoutManager(this)
        val manager = StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL)
        recycleView.layoutManager = manager
        recycleView.adapter = adapter

    }


    //随机生成一个名字
    private fun radmonTitle(str:String):String{
        val n = (1..20).random()
        val s = StringBuilder()
        repeat(n){
            s.append(str)
        }
        return s.toString()
    }


    companion object{
        fun launch(context: Context){
            val intent = Intent(context,SampleRecycleViewActivity::class.java)
            context.startActivity(intent)
        }
    }
}
